<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="会员账号" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="memberUsername">
        <el-input
          v-model="queryParams.memberUsername"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select
          v-model="queryParams.payType"
          placeholder="请选择支付方式"
          clearable
        >
          <el-option
            v-for="dict in pay_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择订单状态"
          clearable
        >
          <el-option
            v-for="dict in order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物流单号" prop="deliverySn">
        <el-input
          v-model="queryParams.deliverySn"
          placeholder="请输入物流单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['order:info:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:info:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="会员账号" align="center" prop="memberId" />
      <el-table-column
        label="订单号"
        align="center"
        prop="orderSn"
        width="300"
      />
      <!-- <el-table-column label="使用的优惠券" align="center" prop="couponId" /> -->
      <el-table-column label="用户名" align="center" prop="memberUsername" />
      <el-table-column label="订单总额" align="center" prop="totalAmount" />
      <el-table-column label="应付总额" align="center" prop="payAmount" />
      <el-table-column label="运费金额" align="center" prop="freightAmount" />
      <!-- <el-table-column
        label="促销优化金额"
        align="center"
        prop="promotionAmount"
      />
      <el-table-column
        label="积分抵扣金额"
        align="center"
        prop="integrationAmount"
      />
      <el-table-column
        label="优惠券抵扣金额"
        align="center"
        prop="couponAmount"
      />
      <el-table-column label="折扣金额" align="center" prop="discountAmount" /> -->
      <el-table-column label="支付方式" align="center" prop="payType">
        <template #default="scope">
          <dict-tag :options="pay_type" :value="scope.row.payType" />
        </template>
      </el-table-column>
      <el-table-column label="订单来源" align="center" prop="sourceType">
        <template #default="scope">
          <dict-tag
            :options="oredr_source_type"
            :value="scope.row.sourceType"
          />
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="物流公司(配送方式)"
        align="center"
        prop="deliveryCompany"
      /> -->
      <el-table-column label="物流单号" align="center" prop="deliverySn" />
      <!-- <el-table-column
        label="自动确认时间"
        align="center"
        prop="autoConfirmDay"
      /> -->
      <!-- <el-table-column
        label="可以获得的积分"
        align="center"
        prop="integration"
      /> -->
      <!-- <el-table-column label="可以获得的成长值" align="center" prop="growth" /> -->
      <!-- <el-table-column label="发票类型" align="center" prop="billType" />
      <el-table-column label="发票抬头" align="center" prop="billHeader" />
      <el-table-column label="发票内容" align="center" prop="billContent" /> -->
      <!-- <el-table-column
        label="收票人电话"
        align="center"
        prop="billReceiverPhone"
      /> -->
      <!-- <el-table-column
        label="收票人邮箱"
        align="center"
        prop="billReceiverEmail"
      /> -->
      <el-table-column label="收货信息" :show-overflow-tooltip="true">
        <template #default="scope">
          <div>
            {{ scope.row.receiverProvince }} / {{ scope.row.receiverCity }} /
            {{ scope.row.receiverRegion }}
            {{ scope.row.receiverDetailAddress }}
          </div>
        </template>
        <!-- <el-table-column
          label="收货人姓名"
          align="center"
          prop="receiverName"
        />
        <el-table-column label="电话" align="center" prop="receiverPhone" />
        <el-table-column label="邮编" align="center" prop="receiverPostCode" />
        <el-table-column
          label="省/直辖市"
          align="center"
          prop="receiverProvince"
        />
        <el-table-column label="城市" align="center" prop="receiverCity" />
        <el-table-column label="区" align="center" prop="receiverRegion" />
        <el-table-column
          label="详细地址"
          align="center"
          prop="receiverDetailAddress"
        /> -->
      </el-table-column>

      <el-table-column label="订单备注" align="center" prop="note" />
      <el-table-column
        label="确认收货状态"
        align="center"
        prop="confirmStatus"
      />
      <el-table-column
        label="使用的积分"
        align="center"
        prop="useIntegration"
      />
      <el-table-column
        label="订单时间"
        align="center"
        prop="paymentTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.paymentTime, "{y}-{m}-{d}") }}</span>
          <div>
            支付时间：{{ parseTime(scope.row.paymentTime, "{y}-{m}-{d}") }}
          </div>
          <div>
            发货时间：{{ parseTime(scope.row.deliveryTime, "{y}-{m}-{d}") }}
          </div>
          <div>
            确认收货时间：{{ parseTime(scope.row.receiveTime, "{y}-{m}-{d}") }}
          </div>
          <div>
            评价时间：{{ parseTime(scope.row.commentTime, "{y}-{m}-{d}") }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Eye"
            @click="clickInfoOpen(scope.row)"
            >详情</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:info:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:info:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderSn">
          <el-input v-model="form.orderSn" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="用户名" prop="memberUsername">
          <el-input v-model="form.memberUsername" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="订单总额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入订单总额" />
        </el-form-item>
        <el-form-item label="应付总额" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入应付总额" />
        </el-form-item>
        <el-form-item label="运费金额" prop="freightAmount">
          <el-input v-model="form.freightAmount" placeholder="请输入运费金额" />
        </el-form-item>
        <el-form-item label="后台调整订单使用的折扣金额" prop="discountAmount">
          <el-input
            v-model="form.discountAmount"
            placeholder="请输入后台调整订单使用的折扣金额"
          />
        </el-form-item>
        <el-form-item label="物流公司(配送方式)" prop="deliveryCompany">
          <el-input
            v-model="form.deliveryCompany"
            placeholder="请输入物流公司(配送方式)"
          />
        </el-form-item>
        <el-form-item label="物流单号" prop="deliverySn">
          <el-input v-model="form.deliverySn" placeholder="请输入物流单号" />
        </el-form-item>
        <el-form-item label="发票抬头" prop="billHeader">
          <el-input v-model="form.billHeader" placeholder="请输入发票抬头" />
        </el-form-item>
        <el-form-item label="发票内容">
          <editor v-model="form.billContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="收票人电话" prop="billReceiverPhone">
          <el-input
            v-model="form.billReceiverPhone"
            placeholder="请输入收票人电话"
          />
        </el-form-item>
        <el-form-item label="收票人邮箱" prop="billReceiverEmail">
          <el-input
            v-model="form.billReceiverEmail"
            placeholder="请输入收票人邮箱"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <!-- 详情 -->
    <el-dialog title="订单详情" width="60%" v-model="infoOpen" append-to-body>
      <el-table :data="form.items">
        <el-table-column label="商品" width="600">
          <template #default="scope">
            <div class="order-item">
              <image-preview
                :width="50"
                :height="50"
                :src="scope.row.skuPic"
              ></image-preview>
              <div class="order-attr">
                <div>{{ scope.row.skuName }}</div>
                <div class="attr-list">
                  <div
                    class="attr"
                    v-for="item in formatJSONToObj(scope.row.skuAttrsVals)"
                    :key="item.attrId"
                    v-if="scope.row.skuAttrsVals"
                  >
                    {{ item.attrName }}：{{ item.attrValue }}；
                  </div>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="单价"
          prop="skuPrice"
          align="center"
        ></el-table-column>
        <el-table-column
          label="数量"
          prop="skuQuantity"
          align="center"
        ></el-table-column>
        <el-table-column
          label="实付"
          prop="realAmount"
          align="center"
        ></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  listOrderInfo,
  getOrderInfo,
  delOrderInfo,
  addOrderInfo,
  updateOrderInfo,
} from "@/api/mall/server/order/info";
const { proxy } = getCurrentInstance();
const { pay_type, order_status, delete_status, oredr_source_type } =
  proxy.useDict(
    "pay_type",
    "order_status",
    "delete_status",
    "oredr_source_type"
  );
const list = ref([]);
const open = ref(false);
const infoOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

const getList = async () => {
  loading.value = true;
  const res = await listOrderInfo(queryParams.value);
  list.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
// json
const formatJSONToObj = (item) => {
  if (item) {
    return JSON.parse(item);
  } else {
    return [];
  }
};
const clickInfoOpen = async (row) => {
  // const res = await getOrderInfo(row.id);
  form.value = row;
  console.log("form.value", form.value);
  infoOpen.value = true;
};
/** 取消按钮 */
const cancel = () => {
  open.value = false;
  reset();
};
/** 表单重置 */
const reset = () => {
  form.value = {
    id: undefined,
    memberId: null,
    orderSn: null,
    couponId: null,
    createTime: null,
    memberUsername: null,
    totalAmount: null,
  };
  proxy.resetForm("formRef");
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加订单信息";
};
/**修改按钮操作 */
const handleUpdate = async (row) => {
  reset();
  const id = row.id || ids.value;
  const res = await getOrderInfo(id);
  form.value = res.data;
  open.value = true;
  title.value = "修改订单信息";
};
/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateOrderInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrderInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};
/** 删除按钮操作 */
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除编号为"' + ids + '"的数据项？')
    .then(function () {
      return delOrderInfo(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};

getList();
</script>

<style lang="scss" scoped>
.price {
  color: rgb(220, 38, 38);
  font-size: 18px;
}
.order-item {
  display: flex;
  align-items: center;
  .order-attr {
    margin-left: 10px;
    .attr-list {
      display: flex;
      flex-wrap: wrap;
    }

    .attr {
      color: #999;
    }
  }
}
</style>
